﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.Drawing;
using SAC.Helper;

public partial class bgData_DLManage : System.Web.UI.Page
{
    DataTable dt = new DataTable();

    BLL.BLLData bd = new BLLData();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { this.Bind(); }
    }

    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        this.GridView2.DataBind();
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sql = "";
        sql = @"insert into 电量测点表(设备ID,测点名称) values('"
            + ((TextBox)this.GridView2.Rows[0].FindControl("txtID")).Text + "','"
            + ((TextBox)this.GridView2.Rows[0].FindControl("txtPName")).Text
            + "');";

        if (bd.RunNonQuery(sql) == true)
        {
            GridView2.EditIndex = -1;
            //num = GridView1.PageSize * GridView1.PageIndex;
            this.Bind();
            this.GridView2.DataBind();
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dt = bd.RetDLPointTable();

        GridView1.PageIndex = e.NewPageIndex;

        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();

        //设置格式
        GridView1.Attributes.Add("BorderColor", "lightBlue");
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        //num = GridView1.PageSize * GridView1.PageIndex;
        this.Bind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (GridView2.Rows.Count > 0)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行添加，禁止删除！')", true);
        }
        else if (GridView1.EditIndex != -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行编辑，禁止删除！')", true);
        }
        else
        {
            string sql = "";
            sql = @"delete from 电量测点表 where ID_KEY = " + GridView1.DataKeys[e.RowIndex].Values[0];

            if (bd.RunNonQuery(sql) == true)
                this.Bind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (GridView2.Rows.Count > 0)
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行添加，禁止编辑！')", true);
        else
        {
            GridView1.EditIndex = e.NewEditIndex;

            //当前编辑行背景色高亮
            this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

            this.Bind();
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sql = "";
        sql = @"update 电量测点表 set 测点名称 = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text
            + "', 设备ID = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text + "'"
            + " where ID_KEY =  " + GridView1.DataKeys[e.RowIndex].Values[0];

        if (bd.RunNonQuery(sql) == true)
        {
            GridView1.EditIndex = -1;

            this.Bind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //加入鼠标滑过的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#E4F6FF',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    protected void LBtnAdd_Click(object sender, EventArgs e)
    {
        if (GridView2.Rows.Count > 0)
        {
        }
        else if (GridView1.EditIndex != -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行编辑，禁止添加！')", true);
        }
        else
        {
            if (this.GridView1.Rows.Count < 1)
                this.GridView2.ShowHeader = true;
            else
                this.GridView2.ShowHeader = false;

            DataTable dt2 = this.GetDataFromGrid();
            DataRow newRow = dt2.NewRow();

            dt2.Rows.Add(newRow);
            this.GridView2.DataSource = dt2;
            this.GridView2.DataBind();
        }
    }

    protected DataTable GetDataFromGrid()
    {
        DataTable dt1 = new DataTable("Table1");
        dt1.Columns.Add("ID_KEY");
        dt1.Columns.Add("CORPID");
        dt1.Columns.Add("CORPNAME");
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            GridViewRow gRow = GridView2.Rows[i];
            DataRow newRow = dt1.NewRow();
            newRow[0] = GridView2.DataKeys[i].Values[0];
            newRow[1] = GridView2.DataKeys[i].Values[1];
            newRow[2] = GridView2.DataKeys[i].Values[2];
            //(gRow.Cells[0].Controls[0] as TextBox).ReadOnly = true; 
            //newRow[1] = (gRow.Cells[1].Controls[0] as TextBox).Text;
            //newRow[2] = (gRow.Cells[2].Controls[0] as TextBox).Text;
            dt1.Rows.Add(newRow);
        }
        dt1.AcceptChanges();
        return dt1;
    }

    private void Bind()
    {

        dt = bd.RetDLPointTable();

        if (dt.Rows.Count > 0)
        {
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            //设置格式
            GridView1.Attributes.Add("BorderColor", "lightBlue");
        }
        else
        {
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

        GridView1.DataKeyNames = new string[] { "ID_KEY", "设备ID", "测点名称" };
    }
}
